package service
{
    import flash.net.SharedObject;
    
    import models.UserVO;
    import models.VO;
    
//	import models.ShopVO2;
//	import models.ThemeFolderVo;
    
    
    public class SOService
    {
        public function SOService()
        {
        }
        
        private static var so:SharedObject;
        
        public static function SO():SharedObject
        {
            if (!so)
                so=SharedObject.getLocal(VO.APPNAME);
            //			so.clear()
            return so;
        }
        
        public static function getShareble():Boolean
        {
            var day:Number=new Date().getDate();
            var shareD:Number=getValue(UserVO.crtUser._id+'_clsShareDay') as Number;
            if(shareD!=day)
                return true;
            else 
                return false;
        }
        
        public static function addRecentAt(list:Array):void
        {
            var arr:Array=getRecentAtList();
            for (var i:int = 0; i < list.length; i++) 
            {
                var di:int=arr.indexOf(list[i]);
                if(di>=0)
                    arr.splice(di,1);
            }
            
            var resultArr:Array;
            if(list.length>=6)
            {
                resultArr=list.slice(0,6);
            }else
            {
                resultArr=list.concat(arr.slice(0,6-list.length));
            }
            
            setValue(UserVO.crtUser._id+'_recentAT',resultArr);
        }
        
        public static function getRecentAtList():Array
        {
            var arr:Array=getValue(UserVO.crtUser._id+'_recentAT') as Array;
            if(!arr)
                arr = [];
            return arr;
        }
        
        public static function setShared():void
        {
            var day:Number=new Date().getDate();
            setValue(UserVO.crtUser._id+'_clsShareDay',day);
        }
        
        /**
         * 0:首页
         * 1:个人中心
         * 2:播放器
         *
         * */
        public static function getGuideStep(step:int):int
        {
            var value:int=getValue('guideStep'+step) as int;
            if(value)
                return value;
            else return 0;
        }
        
        public static function addGuideStep(step:int,value:Object):void
        {
            if(value is Number)
            {
                var v:Number=value as Number;
                v++;
                setValue('guideStep'+step,v);
            }
            else
                setValue('guideStep'+step,value);
        }
        
        public static function getValue(key:String):Object
        {
            return SO().data[key];
        }
        
        public static function setValue(key:String, value:Object):void
        {
            SO().data[key]=value;
            SO().flush();
        }
        
        public static function getUploaded(key:String):Boolean
        {
            var arr:Array=getValue("uploadedArr") as Array;
            if (!arr)
                return false;
            else if (arr.indexOf(key) >= 0)
                return true;
            return false;
        }
        
        public static function setUploaded(key:String):void
        {
            var arr:Array=getValue("uploadedArr") as Array;
            if (!arr)
                arr=[];
            arr.push(key);
            setValue("uploadedArr", arr);
        }
        
//		public static function setDownloaded(o:ShopVO2,save:Boolean):void
//		{
//			var arr:Array=getValue(DOWNLOADED) as Array;
//			if(!arr)
//				arr=[];
//			if(save&&!checkDownloaded(o.path))
//			{
//				arr.push(o);
//			}
//			else
//			{
//				for (var i:int = 0; i < arr.length; i++) 
//				{
//					if(o.path==arr[i].path)
//					{
//						arr.splice(i,1);
//						break;
//					}
//				}
//			}
//			setValue(DOWNLOADED, arr);
//		}
        
        private static var DOWNLOADED:String='downloadedThemes';
        
        public static function checkDownloaded(path:String):Boolean
        {
            var arr:Array=getValue(DOWNLOADED) as Array;
            if(!arr)
                arr=[];
            for each (var o:Object in arr) 
            {
                if(String(o.path).indexOf(path)>=0)
                    return true;
            }
            return false;
        }
        
//		public static function getDownloadedList():Array
//		{
//			var arr:Array=getValue(DOWNLOADED) as Array;
//			if(!arr)
//				arr=[];
//			var result:Array=[];
//
//			for each (var o:Object in arr)
//			{
//				var tfo:ThemeFolderVo=new ThemeFolderVo(o.path,o.num,true);
//				result.push(tfo);
//			}
//
//			return result;
//		}
        
        public static function setBought(path:String,value:Boolean):void
        {
            var b:Boolean=getValue(path+'_bought');
            setValue(path, value);
        }
        
        public static function checkBought(path:String):Boolean
        {
            return getValue(path+'_bought');
        }
        
        /**通过该常量可获取设备账号ID*/
        public static const KEY_DEVICE_ID:String = "deviceID";
        /**通过该常量可获取到登录数据集合，可用于登录接口传参，有可能是第三方平台的登录数据*/
        public static const KEY_LOGIN_DATA:String = "loginData";
        /**通过该常量可获取到存储在本地的用户数据，包含用户名称及当前孩子信息等数据*/
        public static const KEY_USER_DATA:String = "userData";
        /**通过该常量可获取到本地所有cid账号的画作路径及是否已上传*/
        public static const KEY_PAINTS:String = "paints";
        /**通过该常量可获取到已播放过的声音评论id列表*/
        public static const KEY_AUDIO_COMMENTS_PLAYED:String = "audio_comments_played";
        /**通过该常量可获得导航三张图片是否被播放过*/
        public static const KEY_DESCRIBE:String = "describe";
        /**通过该常量可获得指定cid是否已播放过新手指引*/
        public static const KEY_GUIDER:String = "guider";
        /**通过获取到的child_id判断画板的新手引导和解锁*/
        public static const KEY_DRAW:String="draw";
    }
}


